package com.example.himalaya.presenters;

import com.example.himalaya.adapters.PlayerListAdapter;
import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.interfaces.IPlayCallBack;
import com.example.himalaya.interfaces.IPlayPresenter;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.PlayableModel;
import com.ximalaya.ting.android.opensdk.model.advertis.Advertis;
import com.ximalaya.ting.android.opensdk.model.advertis.AdvertisList;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;
import com.ximalaya.ting.android.opensdk.player.advertis.IXmAdsStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.IXmPlayerStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayerException;

import java.util.ArrayList;
import java.util.List;

public class PlayerPresenter implements IPlayPresenter, IXmAdsStatusListener, IXmPlayerStatusListener {
    private  static  final  String TAG = "PlayerPresenter";
    private XmPlayerManager mXmPlayerManager;
    private static  PlayerPresenter sPlayerPresenter;
    private  boolean isPlayListSet = false; //判断播放列表是否被加载出来
    private  List<IPlayCallBack> mIPlayCallBacks = new ArrayList<>(); //建立集合来保存回调状态
    private Track mCurrentTrack;
    private int mCurrentIndex = 0;
    private PlayerListAdapter mSobPopWindow;

    public PlayerPresenter() {
        mXmPlayerManager = XmPlayerManager.getInstance(BaseApplication.getAppContext());
        //广告相关的接口
        mXmPlayerManager.addAdsStatusListener(this);
        //注册播放器相关的接口
        mXmPlayerManager.addPlayerStatusListener(this);

    }
    public  static  PlayerPresenter getPlayerPresenter(){
        if (sPlayerPresenter == null) {
            synchronized (PlayerPresenter.class){
                if (sPlayerPresenter == null){
                    sPlayerPresenter = new PlayerPresenter();
                }
            }
        }
        return  sPlayerPresenter;
    }

    public  void setPlayList(List<Track>list,int playIndex){
        if (mXmPlayerManager != null) {
            mXmPlayerManager.setPlayList(list,playIndex);
            isPlayListSet = true;
            mCurrentTrack = list.get(playIndex);
            mCurrentIndex = playIndex;
        }else {
            LogUtil.d(TAG,"mXmPlayerManager is  null!");
        }
    }

    @Override
    public void play() {
        if (isPlayListSet){
            mXmPlayerManager.play();
        }
    }

    @Override
    public void pause() {
        if (mXmPlayerManager != null) {
            mXmPlayerManager.pause();
        }
    }

    @Override
    public void stop() {
    }

    @Override
    public void playPre() {
        if (mXmPlayerManager != null) {
            mXmPlayerManager.playPre();
        }
    }

    @Override
    public void playNext() {
        if (mXmPlayerManager != null) {
            mXmPlayerManager.playNext();
        }
    }

    @Override
    public void switchPlayMode(XmPlayListControl.PlayMode mode) {
        if (mXmPlayerManager != null) {
            mXmPlayerManager.setPlayMode(mode);
        }
    }

    @Override
    public void getPlayList() {
        if(mXmPlayerManager != null) {
            List<Track> playList = mXmPlayerManager.getPlayList();
            for(IPlayCallBack iPlayCallBack : mIPlayCallBacks) {
                iPlayCallBack.onListLoaded(playList);
            }
        }

    }

    @Override
    public void playByIndex(int index) {
        //切换播放器到指定位置
        if (mXmPlayerManager != null) {
            mXmPlayerManager.play(index);
        }
    }

    @Override
    public void seekTo(int progress) {
        //更新播放器进度
        mXmPlayerManager.seekTo(progress);
    }

    @Override
    public boolean isPlay() {
        //返回当前是否在播放
        return mXmPlayerManager.isPlaying();
    }

    @Override
    public void registerViewCallBack(IPlayCallBack iPlayCallBack) {
        iPlayCallBack.onTrackUpdate(mCurrentTrack,mCurrentIndex);
        if (!mIPlayCallBacks.contains(iPlayCallBack)) {
            mIPlayCallBacks.add(iPlayCallBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(IPlayCallBack iPlayCallBack) {
        mIPlayCallBacks.remove(iPlayCallBack);
    }

    /**
     * -----------------广告相关------------------
     * */
    @Override
    public void onStartGetAdsInfo() {
        LogUtil.e(TAG,"onStartGetAdsInfo ");
    }

    @Override
    public void onGetAdsInfo(AdvertisList advertisList) {
        LogUtil.d(TAG,"onGetAdsInfo ");
    }

    @Override
    public void onAdsStartBuffering() {
        LogUtil.d(TAG,"onAdsStartBuffering ");
    }

    @Override
    public void onAdsStopBuffering() {
        LogUtil.d(TAG,"onAdsStopBuffering ");
    }

    @Override
    public void onStartPlayAds(Advertis advertis, int i) {
        LogUtil.d(TAG,"onStartPlayAds ");
    }

    @Override
    public void onCompletePlayAds() {
        LogUtil.d(TAG,"onCompletePlayAds ");
    }

    @Override
    public void onError(int errorCode, int extra) {
        LogUtil.d(TAG,"onError errorCode --> " + errorCode +"   extra ---> "+extra);
    }

    /**
     * ----------------播放监听的回调方法，主要是监听播放器的状态，并反馈给自定义的回调方法进行图标切换------------
     * */
    @Override
    public void onPlayStart() {
        LogUtil.d(TAG,"onPlayStart ");
        for (IPlayCallBack iPlayCallBack : mIPlayCallBacks){
            iPlayCallBack.onPlayStart();
        }

    }

    @Override
    public void onPlayPause() {
        LogUtil.d(TAG,"onPlayPause ");
        for (IPlayCallBack iPlayCallBack : mIPlayCallBacks){
            iPlayCallBack.onPlayPause();
        }
    }

    @Override
    public void onPlayStop() {
        LogUtil.d(TAG,"onPlayStop ");
        for (IPlayCallBack iPlayCallBack : mIPlayCallBacks){
            iPlayCallBack.onPlayStop();
        }
    }

    @Override
    public void onSoundPlayComplete() {
        LogUtil.d(TAG,"onSoundPlayComplete ");
    }

    @Override
    public void onSoundPrepared() {
        LogUtil.d(TAG,"onSoundPrepared ");
    }

    @Override
    public void onSoundSwitch(PlayableModel lastModel,PlayableModel curModel) {
        LogUtil.d(TAG,"onSoundSwitch...");
        if(lastModel != null) {
            LogUtil.d(TAG,"lastModel..." + lastModel.getKind());
        }
        if(curModel != null) {
            LogUtil.d(TAG,"curModel..." + curModel.getKind());
        }
        //curModel代表的是当前播放的内容
        //通过getKind()方法来获取它是什么类型的
        //track表示是track类型
        //当前节目改变的时候，其海报也要改变
        mCurrentIndex = mXmPlayerManager.getCurrentIndex();
        if(curModel instanceof Track) {
            Track currentTrack = (Track) curModel;
            mCurrentTrack = currentTrack;
            //更新UI
            for(IPlayCallBack iPlayCallback : mIPlayCallBacks) {
                iPlayCallback.onTrackUpdate(mCurrentTrack,mCurrentIndex);
            }
        }

    }

    @Override
    public void onBufferingStart() {
        LogUtil.e(TAG,"onBufferingStart ");
    }

    @Override
    public void onBufferingStop() {
        LogUtil.e(TAG,"onBufferingStop ");
    }

    @Override
    public void onBufferProgress(int progress) {
        LogUtil.e(TAG,"onBufferProgress --- progress---> " +progress);
    }

    @Override
    public void onPlayProgress(int currPos, int duration) {
        //单位毫秒
        for (IPlayCallBack iPlayCallBack : mIPlayCallBacks){
            iPlayCallBack.onProgressChange(currPos,duration);
        }
        //LogUtil.d(TAG,"onPlayProgress --- current Progress--> " +currPos+"   duration -->" +duration);
    }

    @Override
    public boolean onError(XmPlayerException e) {
        LogUtil.d(TAG,"onError ---- e---> " + e);
        return false;
    }
}
